﻿
$(document).ready(function() {
 
    $("#Form1").validate({
        rules: { ddlCategoryId: "required", txtTitle: "required" },
        messages: { ddlCategoryId: " 必填项", txtTitle: " 必填项" }
    });
    //$("#txtTemplateFileName").watermark("如果不设置模板，会自动默认。");

    $("#UP_PicUrl").uploadify({
        height: 30,
        'multi': false,
        swf: '/Scripts/Uploadify/uploadify.swf',
        uploader: '/Scripts/Uploadify/UploadHandler.ashx',
        width: 120,
        cancelImg: '/Scripts/Uploadify/uploadify-cancel.png',
        buttonText: '上传图片',
        fileTypeExts: '*.gif;*.jpg;*.jpeg;*.png',
        fileSizeLimit: '6000KB',
        removeCompleted: true,
        formData: {
            PicW: $("#txtPicW").val(),
            PicH: $("#txtPicH").val(),
            BiLi: $("#cbkBiLi").prop("checked")
        },
        onUploadSuccess: function(file, data, response) {
            //上传完成时触发（每个文件触发一次）
            if (data.indexOf('错误提示') > -1) {
                alert(data);
            } else {
                $("#txtPicUrl").val(data);
            }
        },
        onUploadError: function(file, errorCode, errorMsg, errorString) {
            //当单个文件上传出错时触发
            alert('文件：' + file.name + ' 上传失败: ' + errorString);
        }
    });


    $("#UP_Annex").uploadify({
        height: 30,
        'multi': true,
        swf: '/Scripts/Uploadify/uploadify.swf',
        uploader: '/api/Annex/Upload',
        width: 120,
        cancelImg: '/Scripts/Uploadify/uploadify-cancel.png',
        buttonText: '上传附件',
        fileTypeExts: '*.zip;*.rar;*.dwg;*.doc;*.chm;*.pdf;*.gif;*.jpg;*.png;*.swf',
        fileSizeLimit: '6000KB',
        removeCompleted: true,
        'debug': false,
        formData: {
            ContentId: Request.QueryString("ID") == null ? -1 : Request.QueryString("ID"),
            ModelId: Request.QueryString("ModelId"),
            TempFlag: Request.QueryString("TempFlag") == null ? "" : Request.QueryString("TempFlag")
        },
        onUploadSuccess: function(file, data, response) {
            if (data.indexOf('错误提示') > -1) {
                alert(data);
            } else {
                var info = $.parseJSON(data);
                $("#T_AnnexList tbody").append("<tr><td>" + info.FileName + "</td><td>" + info.DownURL + "</td><td style=\"text-align:center\"><a href=\"javascript:void(0)\" onclick=\"deleteAnnex(this," + info.Id + ");\">删除</a></td></tr>");
            }
        },
        onUploadError: function(file, errorCode, errorMsg, errorString) {
            alert('文件：' + file.name + ' 上传失败: ' + errorString);
        }
    });


    laodAnnex();
});



function GetImage()
{
    $("#dialog-modal").dialog({
        height: 540,
        width: 540,
        title: "图片浏览",
        modal: true
    });
    var urlC = $("#dialog-modal-url");
    urlC.attr("src", "../window/GetImage.aspx");
    urlC.width(500);
    urlC.height(500);
}
function GetTemplate()
{
    $("#dialog-modal").dialog({
        height: 460,
        width: 500,
        title: "网页模板",
        modal: true
    });
    var urlC = $("#dialog-modal-url");
    urlC.attr("src", "../window/GetTemplate.aspx?Control=txtTemplateFileName");
    urlC.width(460);
    urlC.height(410);
}
function GetTag(modelId)
{
    $("#dialog-modal").dialog({
        height: 460,
        width: 500,
        title: "Tags选用",
        modal: true
    });
    var urlC = $("#dialog-modal-url");
    urlC.attr("src", "../window/GetTag.aspx?ModelId=" + modelId + "&Control=txtKey");
    urlC.width(460);
    urlC.height(410);
}

function AuthorMore()
{
    var urlC = $("#dialog-modal-url");
    urlC.attr("src", "../window/GetContentProperty.aspx?ModelId=" + Request.QueryString("ModelId") + "&Type=1&Control=txtAuthor");
    urlC.width(460);
    urlC.height(410);

    $("#dialog-modal").dialog({
        height: 460,
        width: 500,
        title: "作者选用",
        modal: true
    });
}

function FromMore()
{
    var urlC = $("#dialog-modal-url");
    urlC.attr("src", "../window/GetContentProperty.aspx?ModelId=" + Request.QueryString("ModelId") + "&Type=2&Control=txtFrom");
    urlC.width(460);
    urlC.height(410);

    $("#dialog-modal").dialog({
        height: 460,
        width: 500,
        title: "文章来源",
        modal: true
    });
}

function CheckTitle()
{
    $.ajax({
        type: "post",
        dataType: "JSON",
        url: "../DataHandler.ashx?Action=CheckTitle&Title=" + escape($("#txtTitle").val()) + "&ModelId=" + Request.QueryString("ModelId"),
        beforeSend: function ()
        {

        },
        success: function (data)
        {
            alert(data.Msg);
        }
    });
}

function laodAnnex()
{
    var id = Request.QueryString("ID") == null ? 0 : Request.QueryString("ID");
    var tempFlag = Request.QueryString("TempFlag") == null ? "" : Request.QueryString("TempFlag");
    $.ajax({
        type: "get",
        dataType: "JSON",
        url: "/api/Annex/GetList?ContentId=" + id + "&TempFlag=" + tempFlag,
        success: function (data)
        {
            for (var i = 0; i < data.length; i++)
            {
                var info = data[i];
                $("#T_AnnexList tbody").append("<tr><td>" + info.FileName + "</td><td>" + info.DownURL + "</td><td style=\"text-align:center\"><a href=\"javascript:void(0)\" onclick=\"deleteAnnex(this," + info.Id + ");\">删除</a></td></tr>");
            }
        }
    });
}

function deleteAnnex(obj, id)
{
    var downUrl = $(obj).parent().prev().html();
    $.ajax({
        type: "delete",
        dataType: "JSON",
        url: "/api/Annex/Delete?ID=" + id + "&DownURL=" + escape(downUrl),
        success: function (Data)
        {
            $(obj).parent().parent().remove();
        }
    });
}